package com.foodapp.back.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.foodapp.back.common.Result;
import com.foodapp.back.dto.RestaurantDTO;
import com.foodapp.back.entity.Restaurant;
import com.foodapp.back.service.RestaurantService;
import com.foodapp.back.vo.RestaurantVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 管理员餐厅控制器
 */
@RestController
@RequestMapping("/admin/restaurant")
public class AdminRestaurantController {

    @Resource
    private RestaurantService restaurantService;

    /**
     * 分页查询餐厅列表
     */
    @GetMapping("/list")
    public Result<Page<RestaurantVO>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status) {
        
        Page<Restaurant> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Restaurant> wrapper = new LambdaQueryWrapper<>();
        
        // 查询条件
        if (StringUtils.isNotBlank(category)) {
            wrapper.eq(Restaurant::getCategory, category);
        }
        
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like(Restaurant::getName, keyword)
                    .or().like(Restaurant::getDescription, keyword);
        }
        
        if (status != null) {
            wrapper.eq(Restaurant::getStatus, status);
        }
        
        // 按创建时间降序排序
        wrapper.orderByDesc(Restaurant::getCreateTime);
        
        // 查询分页数据
        Page<Restaurant> restaurantPage = restaurantService.page(pageParam, wrapper);
        
        // 转换为VO
        Page<RestaurantVO> result = new Page<>();
        BeanUtils.copyProperties(restaurantPage, result, "records");
        
        List<RestaurantVO> restaurantVOList = restaurantPage.getRecords().stream().map(restaurant -> {
            RestaurantVO restaurantVO = new RestaurantVO();
            BeanUtils.copyProperties(restaurant, restaurantVO);
            return restaurantVO;
        }).collect(Collectors.toList());
        
        result.setRecords(restaurantVOList);
        
        return Result.success(result);
    }

    /**
     * 获取所有餐厅
     */
    @GetMapping
    public Result<List<RestaurantVO>> listAll() {
        List<Restaurant> restaurants = restaurantService.list();
        List<RestaurantVO> result = restaurants.stream().map(restaurant -> {
            RestaurantVO restaurantVO = new RestaurantVO();
            BeanUtils.copyProperties(restaurant, restaurantVO);
            return restaurantVO;
        }).collect(Collectors.toList());
        return Result.success(result);
    }

    /**
     * 获取餐厅详情
     */
    @GetMapping("/{id}")
    public Result<RestaurantVO> getById(@PathVariable Long id) {
        RestaurantVO restaurantVO = restaurantService.getRestaurantById(id);
        return Result.success(restaurantVO);
    }

    /**
     * 新增餐厅
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody @Valid RestaurantDTO restaurantDTO) {
        boolean result = restaurantService.addRestaurant(restaurantDTO);
        return Result.success(result);
    }

    /**
     * 更新餐厅
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody @Valid RestaurantDTO restaurantDTO) {
        boolean result = restaurantService.updateRestaurant(restaurantDTO);
        return Result.success(result);
    }

    /**
     * 删除餐厅
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = restaurantService.deleteRestaurant(id);
        return Result.success(result);
    }

    /**
     * 更新餐厅状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean result = restaurantService.updateStatus(id, status);
        return Result.success(result);
    }
}
